﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using ClientDomain.Commnads;
using ClientDomain.Properties;
using Comunication;
using Utiles;

namespace ClientDomain
{
    public class UdpClientPackage : IUdpPacketProcessor
    {
        #region Implementation of IUdpUtils

        internal int bytesProcesados;
        public void ProcessPacket(byte[] buffer, IPEndPoint remoteEndpoint)
        {
            UTF8Encoding decoder = new UTF8Encoding();
            string header = decoder.GetString(buffer, 0, 3);
            string cmd = decoder.GetString(buffer, 3, 2);
            int dataLength = int.Parse(decoder.GetString(buffer, 5, 5));
            string data = decoder.GetString(buffer, 10, dataLength);

            Commands command = (Commands)int.Parse(cmd);

            switch (command)
            {
                case Commands.UPLOAD_SERVER_CLIENT_FIND_SERVER_RESPONSE:
                    if (header.Equals("RES"))
                    {
                        string puertoTcp = Utilidades.ObtenerInformacion("PUERTOTCP", data,';');
                        TcpClientPackage packetProcessor = new TcpClientPackage();
                        Socket socket =  TCPClient.ConnectToServer(Convert.ToInt32(puertoTcp),remoteEndpoint.Address,packetProcessor, Convert.ToInt32(Settings.Default.MaxBandwith));
                        ClientSendFileInformation clientSendFileInformation = new ClientSendFileInformation(ClientAccessComunication.FileName);
                        TCPClient.SendCommand(socket, clientSendFileInformation);
                    }
                    break;
                case Commands.DOWNLOAD_SERVER_CLIENT_FIND_SERVER_RESPONSE:
                    if (header.Equals("RES"))
                    {
                        if (Convert.ToBoolean(Utilidades.ObtenerInformacion("EXISTS", data, ';')))
                        {

                            TcpClientPackage packetProcessor = new TcpClientPackage
                            {
                                MD5 = Utilidades.ObtenerInformacion("MD5", data, ';'),
                                FileName = Utilidades.ObtenerInformacion("FILENAME", data, ';'),
                                cantBytesProcesados = Convert.ToInt32(Utilidades.ObtenerInformacion("BYTES", data, ';'))
                            };

                            var tcpPort = Utilidades.ObtenerInformacion("TCPPORT", data, ';');

                            Socket socket = TCPClient.ConnectToServer(Convert.ToInt32(tcpPort), remoteEndpoint.Address, packetProcessor, Convert.ToInt32(Settings.Default.MaxBandwith));
                            DownloadClientServerInformationResponse downloadClientServerInformationResponse =
                                new DownloadClientServerInformationResponse(packetProcessor.FileName, packetProcessor.cantBytesProcesados);
                            TCPClient.SendCommand(socket, downloadClientServerInformationResponse);
                        }
                    }
                    break;
            }
        }


        #endregion
    }
}
